--- title: SRCNN+BN: Image super-resolution using deep convolutional networks + Batch Normalization keywords: fastai sidebar: home_sidebar ---
{% raw %}
{% endraw %} {% raw %}
%reload_ext autoreload
%autoreload 2
%matplotlib inline
{% endraw %} {% raw %}
{% endraw %} {% raw %}
import sys
sys.path.append('..')
from superres.datasets import *
from superres.databunch import *
{% endraw %} {% raw %}
seed = 8610
random.seed(seed)
np.random.seed(seed)
{% endraw %}

SRCNN: Image super-resolution using deep convolutional networks

{% raw %}
{% endraw %} {% raw %}

class SRCNN_BN[source]

SRCNN_BN() :: Module

Image super-resolution using deep convolutional networks

{% endraw %}

DataBunch

{% raw %}
train_hr = div2k_train_hr_crop_256
{% endraw %} {% raw %}
in_size = 256
out_size = 256
scale = 4
bs = 10
{% endraw %} {% raw %}
data = create_sr_databunch(train_hr, in_size=in_size, out_size=out_size, scale=scale, bs=bs, seed=seed)
print(data)
data.show_batch()
ImageDataBunch;

Train: LabelList (25245 items)
x: ImageImageList
Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256)
y: ImageImageList
Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256)
Path: /home/jovyan/notebook/datasets/DIV2K/DIV2K_train_HR_crop/256;

Valid: LabelList (6311 items)
x: ImageImageList
Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256)
y: ImageImageList
Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256),Image (3, 256, 256)
Path: /home/jovyan/notebook/datasets/DIV2K/DIV2K_train_HR_crop/256;

Test: None
{% endraw %}

Training

{% raw %}
model = SRCNN_BN()
loss_func = MSELossFlat()
metrics = [m_psnr, m_ssim]
learn = Learner(data, model, loss_func=loss_func, metrics=metrics)
model_name = model.__class__.__name__
{% endraw %} {% raw %}
lr_find(learn)
learn.recorder.plot(suggestion=True)
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
Min numerical gradient: 4.79E-04
Min loss divided by 10: 2.29E-01
{% endraw %} {% raw %}
lr = 1e-3
lrs = slice(lr)
epoch = 3
pct_start = 0.3
wd = 1e-3
save_fname = model_name
{% endraw %} {% raw %}
callbacks = [ShowGraph(learn), SaveModelCallback(learn, name=save_fname)]
{% endraw %} {% raw %}
learn.fit_one_cycle(epoch, lrs, pct_start=pct_start, wd=wd, callbacks=callbacks)
epoch train_loss valid_loss m_psnr m_ssim time
0 0.201926 0.067097 29.948057 0.431795 02:05
1 0.199461 0.064298 30.555027 0.449006 02:17
2 0.200075 0.064046 30.100824 0.447933 02:01
Better model found at epoch 0 with valid_loss value: 0.06709658354520798.
Better model found at epoch 1 with valid_loss value: 0.06429817527532578.
Better model found at epoch 2 with valid_loss value: 0.06404571235179901.
{% endraw %} {% raw %}
learn.show_results()
{% endraw %}

Test

{% raw %}
test_hr = set14_hr
{% endraw %} {% raw %}
il_test_x = ImageImageList.from_folder(test_hr, after_open=partial(after_open_image, size=out_size, scale=4, sizeup=True))
il_test_y = ImageImageList.from_folder(test_hr, after_open=partial(after_open_image, size=out_size))
{% endraw %} {% raw %}
_ = learn.load(save_fname)
{% endraw %} {% raw %}
sr_test(learn, il_test_x, il_test_y, model_name)
bicubic: PSNR:24.11,SSIM:0.7822
SRCNN_BN:	 PSNR:24.40,SSIM:0.7991
{% endraw %}

Report

{% raw %}
model
SRCNN_BN(
  (conv1): Sequential(
    (0): Conv2d(3, 64, kernel_size=(9, 9), stride=(1, 1), padding=(4, 4), bias=False)
    (1): ReLU(inplace=True)
    (2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  )
  (conv2): Sequential(
    (0): Conv2d(64, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (1): ReLU(inplace=True)
    (2): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  )
  (conv3): Sequential(
    (0): Conv2d(32, 3, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), bias=False)
    (1): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  )
)
{% endraw %} {% raw %}
learn.summary()
SRCNN_BN
======================================================================
Layer (type)         Output Shape         Param #    Trainable 
======================================================================
Conv2d               [64, 256, 256]       15,552     True      
______________________________________________________________________
ReLU                 [64, 256, 256]       0          False     
______________________________________________________________________
BatchNorm2d          [64, 256, 256]       128        True      
______________________________________________________________________
Conv2d               [32, 256, 256]       2,048      True      
______________________________________________________________________
ReLU                 [32, 256, 256]       0          False     
______________________________________________________________________
BatchNorm2d          [32, 256, 256]       64         True      
______________________________________________________________________
Conv2d               [3, 256, 256]        2,400      True      
______________________________________________________________________
BatchNorm2d          [3, 256, 256]        6          True      
______________________________________________________________________

Total params: 20,198
Total trainable params: 20,198
Total non-trainable params: 0
Optimized with 'torch.optim.adam.Adam', betas=(0.9, 0.99)
Using true weight decay as discussed in https://www.fast.ai/2018/07/02/adam-weight-decay/ 
Loss function : FlattenedLoss
======================================================================
Callbacks functions applied 
{% endraw %}